<?php

declare (strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Model;

/**
 * @property int $id 
 * @property string $user_name 
 * @property string $password 
 * @property string $head 
 * @property int $login_times 
 * @property string $last_login_ip 
 * @property int $last_login_time 
 * @property string $real_name 
 * @property int $status 
 * @property int $role_id 
 */
class Admin extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'admin';

    public $timestamps = false;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'login_times' => 'integer', 'last_login_time' => 'integer', 'status' => 'integer', 'role_id' => 'integer'];

    /**
     * 获取单个用户
     * @param $where
     * @param string $field
     * @return array
     */
    public function getOne($where,$field="*")
    {
        if(is_array($where)){
            $res =  $this->where($where)->select($field=='*'?'*':explode(',',$field))->first();
            if(!empty($res)){
                return $res->toArray();
            }
        }else{
            $res =  $this->whereRaw($where)->select($field=='*'?'*':explode(',',$field))->first();
            if(!empty($res)){
                return $res->toArray();
            }
        }
    }
    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Query\Builder
     */
    public function getUsers($where=[],$field='*',$limit=10)
    {
        if(is_array($where)){
            $res =  $this->where($where)->select($field=='*'?'*':explode(',',$field))->paginate(intval($limit));
            if(!empty($res)){
                return for_paginator_data_formater($res);
            }
        }else{
            $res =  $this->whereRaw($where)->select($field=='*'?'*':explode(',',$field))->paginate(intval($limit));
            if(!empty($res)){
                return for_paginator_data_formater($res);
            }
        }
    }

    public function add($data)
    {
        return $this->insertGetId($data);
    }

    public function edit($id,$dada)
    {
        return $this->where('id',$id)->update($dada);
    }
}